/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


//    var date = new Date();
//    date.setTime(date.getTime()+(0*24*60*60*1000));
//    var expires = "; expires="+date.toGMTString();
    
//function confirmQuantity(){
//    var x;
//    var quantity = prompt("Please enter quantity of product","1");
//    if (!isNaN(quantity))
//    {
//        x = quantity;
//    }
//
//    return x;
//}
function addCart(elem){//dung trong truong hop lay thong tin san pham trong xsl
    var flag;
    var cookievalue =  "";
    var item = document.cookie.split(";");
    if(item[0] == ",,0," || item[0] == ""){
        document.cookie = document.cookie.replace(item[0], "", true);
    }
    item = document.cookie.split(";");
    if(item[1] == undefined){
        var nodeProduct = elem.parentNode.parentNode;
        var id = nodeProduct.getElementsByClassName("productID")[0].innerHTML;
        var name = nodeProduct.getElementsByClassName("productName")[0].innerHTML;
        var price = nodeProduct.getElementsByClassName("productPrice")[0].innerHTML;
        //        var quantity = confirmQuantity();
        var quantity = 1;
        cookievalue += name+","+price+","+quantity+","+id;
        document.cookie = cookievalue;
    }else{
        var itemInfo = item[1].split(",");
        var tmp;
        var tmp2;
        if(itemInfo[0] == " "){
            nodeProduct = elem.parentNode.parentNode;
            id = nodeProduct.getElementsByClassName("productID")[0].innerHTML;
            name = nodeProduct.getElementsByClassName("productName")[0].innerHTML;
            price = nodeProduct.getElementsByClassName("productPrice")[0].innerHTML;
            quantity = 1;
            cookievalue += name+","+price+","+quantity+","+id;
            document.cookie = document.cookie.replace(" ,0,0", "", true);
            document.cookie=cookievalue;
        }else {
            nodeProduct = elem.parentNode.parentNode;
            id = nodeProduct.getElementsByClassName("productID")[0].innerHTML;
            name = nodeProduct.getElementsByClassName("productName")[0].innerHTML;
            price = nodeProduct.getElementsByClassName("productPrice")[0].innerHTML;
            var arrayProduct = item[1].split(".");
            for(var i = 0; i<arrayProduct.length; i++){
                var arrayInfo = arrayProduct[i].split(",");
                if(id == arrayInfo[3]){
                    flag = true;
                    break;
                }
                flag = false;                
            }
            if(flag == true){
                cookievalue = "";
                tmp = arrayProduct.slice(0, i);
                name = arrayInfo[0];
                price = arrayInfo[1];
                //                quantity = parseInt(parseInt(quantity) + parseInt(arrayInfo[2]));
                quantity = parseInt(parseInt(arrayInfo[2]) + parseInt(1));
                tmp2 = name+","+price+","+quantity+","+id;
                //tmp2 = [name,price,quantity,id];
                tmp.push(tmp2);
                for(var j = i+1; j < arrayProduct.length; j++){
                    tmp.push(arrayProduct[j]);
                }
                arrayProduct = tmp;
                for(var k = 0; k<arrayProduct.length-1;k++){
                    cookievalue += arrayProduct[k] + ".";
                }
                item[1] = cookievalue + arrayProduct[arrayProduct.length-1];
            }else{
                quantity = 1;
                cookievalue += name+","+price+","+quantity+","+id;
                item[1] = item[1] +"."+ cookievalue;
            }
            //        cookievalue += name+","+price+","+quantity+","+id;
            //        item[1] = item[1] +"."+ cookievalue;
            document.cookie = item[1];
        }
    }
    var count = document.cookie.split(";");
    var countlength = count[1].split(".");
    document.getElementById("count").innerHTML = countlength.length;
}

function addCart1(idP){//dung trong truong hop lay thong tin san pham binh thuong trong trang jsp
    var flag;
    var tmp;
    var tmp2;
    var cookievalue =  "";
    var item = document.cookie.split(";");
    if(item[0] == ",,0," || item[0] == ""){
        document.cookie = document.cookie.replace(item[0], "", true);
    }
    item = document.cookie.split(";");
    if(item[1] == undefined){
        //var id = document.getElementsByClassName("pID")[0].value;
        var id = document.getElementById(idP + "_productId").value;
        var name = document.getElementById(idP + "_productName").innerHTML;
        var price = document.getElementById(idP + "_productPrice").innerHTML;
        //        var quantity = confirmQuantity();
        var quantity = 1;
        cookievalue += name+","+price+","+quantity+","+id;
        document.cookie = cookievalue;
    }
    var itemInfo = item[1].split(",");
    if(itemInfo[0] == " "){
        id = document.getElementById(idP + "_productId").value;
        name = document.getElementById(idP + "_productName").innerHTML;
        price = document.getElementById(idP + "_productPrice").innerHTML;
        quantity = 1;
        cookievalue += name+","+price+","+quantity+","+id;
        document.cookie = document.cookie.replace(" ,0,0", " ,0,0,0", true);
        document.cookie=cookievalue;
    }else {
        id = document.getElementById(idP + "_productId").value;
        name = document.getElementById(idP + "_productName").innerHTML;
        price = document.getElementById(idP + "_productPrice").innerHTML;
        quantity = 1;
        var arrayProduct = item[1].split(".");
        for(var i = 0; i<arrayProduct.length; i++){
            var arrayInfo = arrayProduct[i].split(",");
            if(id == arrayInfo[3]){
                flag = true;
                break;
            }
            flag = false;
        }
        if(flag == true){
            cookievalue = "";
            tmp = arrayProduct.slice(0, i);
            name = arrayInfo[0];
            price = arrayInfo[1];
            quantity = parseInt(parseInt(arrayInfo[2]) + parseInt(1));
            tmp2 = name+","+price+","+quantity+","+id;
            //tmp2 = [name,price,quantity,id];
            tmp.push(tmp2);
            for(var j = i+1; j < arrayProduct.length; j++){
                tmp.push(arrayProduct[j]);
            }
            arrayProduct = tmp;
            for(var k = 0; k<arrayProduct.length-1;k++){
                cookievalue += arrayProduct[k] + ".";
            }
            item[1] = cookievalue + arrayProduct[arrayProduct.length-1];
        }else{
            quantity = 1;
            cookievalue += name+","+price+","+quantity+","+id;
            item[1] = item[1] +"."+ cookievalue;
        }
        //        cookievalue += name+","+price+","+quantity+","+id;
        //        item[1] = item[1] +"."+ cookievalue;
        document.cookie = item[1];
    }
    var count = document.cookie.split(";");
    var countlength = count[1].split(".");
    document.getElementById("count").innerHTML = countlength.length;
}

function updateCart(id){
    var tmp;
    var tmp2;
    var arrayInfo;
    var a = document.cookie.split(";");
    var cookievalue2 ="";
    var arrayProduct = a[1].split(".");
    if(arrayProduct.length>1){
        for(var i = 0; i<arrayProduct.length; i++){
            arrayInfo = arrayProduct[i].split(",");
            if(id == arrayInfo[3]){
                tmp = arrayProduct.slice(0, i);
                var name = arrayInfo[0];
                var price = arrayInfo[1];
                var quantity = document.getElementById(id + "_quantity").value;
                tmp2 = name+","+price+","+quantity+","+id;
                //tmp2 = [name,price,quantity,id];
                tmp.push(tmp2);
                for(var j = i+1; j < arrayProduct.length; j++){
                    tmp.push(arrayProduct[j]);
                }
                arrayProduct = tmp;
            }
        }
        for(var k = 0; k<arrayProduct.length-1;k++){
            cookievalue2 += arrayProduct[k] + ".";
        }
        cookievalue2 = cookievalue2 + arrayProduct[arrayProduct.length-1];        
    }else{
        arrayInfo = arrayProduct[0].split(",");
        name = arrayInfo[0];
        price = arrayInfo[1];
        quantity =  arrayInfo[2]= document.getElementById(id +"_quantity").value;
        cookievalue2 = name + "," + price+","+quantity+","+id;
    }
    document.cookie=cookievalue2;
    //window.location="cartPage.jsp";
    //check count cart
    countCart();
    loadCart();
}
function deleteAllCart(){
    //    var date = new Date();
    //    date.setTime(date.getTime()+(-1*24*60*60*1000));
    //    var expires = "; expires="+date.toGMTString();
    var a = document.cookie.split(";");
    var arrayProduct = a[1].split(".");
    var cookievalue3="";
    var arrayInfo;
    arrayProduct = arrayProduct.slice(0, 1);
    cookievalue3 = cookievalue3 + arrayProduct[0];
    arrayInfo = arrayProduct[0].split(",");
    var name = arrayInfo[0] = "";
    var price = arrayInfo[1] = "";
    var quantity =  arrayInfo[2]= 0;
    var idProduct = arrayInfo[3] = "";
    cookievalue3 = name + "," + price+","+quantity+","+idProduct;
    document.cookie = cookievalue3;
    //window.location="cartPage.jsp";

    //check count cart
    countCart();
    loadCart();
}
function deleteItem(id){
    //    var date = new Date();
    //    date.setTime(date.getTime()+(-1*24*60*60*1000));
    //    var expires = "; expires="+date.toGMTString();
    var tmp;
    var arrayInfo;
    var cookievalue3="";
    var a = document.cookie.split(";");
    var arrayProduct = a[1].split(".");//cai chuoi de lay mang cac
    if(arrayProduct.length>1){
        for(var i = 0; i<arrayProduct.length; i++){
            arrayInfo = arrayProduct[i].split(",");//cat chuoi de lay thong tin cua 1 product
            if(id == arrayInfo[3]){//c[3] la id cua product
                tmp = arrayProduct.slice(0,i);
                for(var j = i+1; j<arrayProduct.length; j++){
                    tmp.push(arrayProduct[j]);
                }
                arrayProduct=tmp;
            }
        }
        for(var k = 0; k<arrayProduct.length-1;k++){
            cookievalue3 += arrayProduct[k] + ".";
        }
        cookievalue3 = cookievalue3 + arrayProduct[arrayProduct.length-1];
    }else{
        arrayInfo = arrayProduct[0].split(",");
        var name = arrayInfo[0] = "";
        var price = arrayInfo[1] = "";
        var quantity =  arrayInfo[2]= 0;
        var idProduct = arrayInfo[3] = "";
        cookievalue3 = name + "," + price+","+quantity+","+idProduct;
    }

    document.cookie=cookievalue3;
    //window.location="cartPage.jsp";

    //check count of cart
    countCart();
    loadCart();
}
function loadCart(){
    var cookie = document.cookie.split(";");
    var testBug = cookie[1].split("=");
    if(testBug[0] == " JSESSIONID"){
        document.cookie = document.cookie.replace(cookie[1], cookie[0], true);
    }
    if(cookie[0] == ",,0," || cookie[0] == ""){
        document.cookie = document.cookie.replace(cookie[0], "", true);
    }
    cookie = document.cookie.split(";");
    if(cookie[1] != undefined){
        var items = cookie[1].split(".");
        var total=0;
        var cartList ="";
        for(var i=0;i<items.length;i++)
        {
            var itemInfo=items[i].split(",");
            var itemInfo0 = itemInfo[0];
            var itemInfo1 = itemInfo[1];
            var itemInfo2 = itemInfo[2];
            var itemInfo3 = itemInfo[3];
            
            if(itemInfo0 == " "){
                cartList = cartList + "<input type='hidden' id='content-Cart-ID' name='productID' value=''/><div id='content-Cart-Product'></div><div id='content-Cart-Price'></div><div id='content-Cart-Quantity'><input type='hidden' id='quantity' value='"+itemInfo2+"' size='5' onkeyup='updateCart()'></div>" +
                "<div id='content-Cart-Delete'><input id='deletebtn' type='hidden' name='btnAction' value='Delete' onclick='deleteItem()'></div>";
            }else{
                cartList = cartList + "<input type='hidden' id='content-Cart-ID' name='productID' value='"+ itemInfo3 +"'/><div id='content-Cart-Product'>"+itemInfo0+"</div>" +
                "<div id='content-Cart-Price'>"+itemInfo1+"</div>" +
                "<div id='content-Cart-Quantity'><input type='text' id='"+itemInfo3+"_quantity' maxlength='3' value='"+itemInfo2+"' size='5' onchange='updateCartFunc(\"" + itemInfo3 + "\")'></div>" +
                "<div id='content-Cart-Delete'><button id='deletebtn' type='button' name='btnAction' value='Delete' onclick='deleteItem("+itemInfo3+")'>Xóa</button></div>";

            }
            document.getElementById("COname").value = itemInfo[0];
            document.getElementById("COprice").value = itemInfo[1];
            document.getElementById("COquantity").value = itemInfo[2];
            document.getElementById("COid").value = itemInfo[3];
            document.getElementById("COProductList").value = cookie[1];
            total += parseFloat(itemInfo[1]*itemInfo[2]);        
        }
        document.getElementById("divContentCart").innerHTML = cartList;
        if(!isNaN(total)){
            if(total != 0 ){
                document.getElementById("total").innerHTML=total;
            }
            if(itemInfo0 == " "){
                document.getElementById("divTotalCart").style.display="none";
            }
        }
        document.getElementById("COtotal").value=total;
    }
}
